//
//  QrCodeController.swift
//  SCA
//
//  Created by tjun on 2025/9/6.
//

import UIKit
import SnapKit

class QrCodeController: BaseController {

    private let referrerCode = UserInfoModel.current.referrerCode
    private lazy var shareLinkUrl = referrerCode
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        printf("shareLinkUrl=\(shareLinkUrl)")
        
        let backgroundView = UIImageView(image: UIImage(named: "qrCodeBigBg")).addTo(view)
        backgroundView.contentMode = .scaleAspectFill
        backgroundView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
  
        let contentView = UIView().addTo(self.view)
        contentView.addCorner(radius: 22)
        contentView.backgroundColor = UIColorMake(0x0E0F13)
        contentView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.height.equalTo(510)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-20)
        }
        
        let backButton = UIButton(image: UIImage(named: "nav_back"), target: self, action: #selector(popViewController)).addTo(contentView)
        backButton.snp.makeConstraints { make in
            make.left.equalTo(10)
            make.width.height.equalTo(30)
            make.top.equalTo(10)
        }
        
        let titleLabel = UILabel(text: "分享好友".localizedString, font: UIFontSemiboldMake(25), textColor: .white).addTo(contentView)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(30)
        }
        
        let qrImage = UIImage.generateQRCode(from: referrerCode, size: 240)
        let qrCodeImageView = UIImageView(image: qrImage)
        contentView.addSubview(qrCodeImageView)
        qrCodeImageView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(100)
            make.width.height.equalTo(240)
        }
   
        let copyButton = UIButton(image: UIImage(named: "share_copy"), target: self, action: #selector(copyButtonTapped)).addTo(contentView)
        copyButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview().offset(40)
            make.top.equalTo(qrCodeImageView.snp.bottom).offset(20)
            make.height.width.equalTo(30)
        }
        
        let usernameLabel = UILabel(text: referrerCode, font: UIFontMediumMake(20), textColor: .white).addTo(contentView)
        usernameLabel.snp.makeConstraints { make in
            make.right.equalTo(copyButton.snp.left).offset(-20)
            make.centerY.equalTo(copyButton)
        }
        
        let linkView = addLineView().addTo(contentView)
        linkView.addCorner(radius: 10)
        linkView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalTo(usernameLabel.snp.bottom).offset(30)
        }

    }
    
    private func addLineView() -> UIView {
        let linkView = UIView()
        linkView.backgroundColor = UIColorMake(0x2E3036)
        
        let linkCopyButton = UIButton(image: UIImage(named: "share_copy"), target: self, action: #selector(linkCopyButtonTapped)).addTo(linkView)
        linkCopyButton.snp.makeConstraints { make in
            make.right.equalTo(-20)
            make.centerY.equalToSuperview()
            make.height.width.equalTo(30)
        }
        
        let linkLabel = UILabel(text: shareLinkUrl, font: UIFontMediumMake(12), textColor: UIColorMake(0xCCCCCC)).addTo(linkView)
        linkLabel.attributedText = linkLabel.text?.titleMargin(3, alignment: .left)
        linkLabel.numberOfLines = 0
        linkLabel.snp.makeConstraints { make in
            make.right.equalTo(linkCopyButton.snp.left).offset(-20)
            make.left.equalTo(20)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview().offset(-16)
        }
        
        return linkView
    }
    
    @objc func copyButtonTapped() {
        UIPasteboard.general.string = referrerCode
        HUD.showMessage("复制成功".localizedString)
    }
    
    @objc func linkCopyButtonTapped() {
        UIPasteboard.general.string = shareLinkUrl
        HUD.showMessage("复制成功".localizedString)
    }
    
    override var hiddenNavigationBar: Bool {
        return true
    }
    
}
